package com.lijianpo.util.extension.api;

import com.lijianpo.util.extension.enums.ErrorCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.List;

/**
 * 分页数据通用返回格式
 *
 * @param <T>
 * @author: zhangyang
 * @since: 2019年7月5日上午11:03:00
 */
@ApiModel
public class PageResult<T> extends DataResult<T> {

	/**
	 * 是否有下一页
	 */
	@ApiModelProperty(value = "是否有下一页")
	protected boolean hasNext = true;

	/**
	 * 当前页数
	 */
	@ApiModelProperty(value = "当前页数")
	protected int pageNum;

	/**
	 * 分页数据
	 */
	/** 响应中的对象. */
	@ApiModelProperty(value = "分页数据")
	protected List<T> listData;

	/**
	 * 数据总量
	 */
	@ApiModelProperty(value = "数据总量")
	protected int total;

	public PageResult() {
		this.code = ErrorCodeEnum.OK.getResponseStatusCode() + "";
		this.miniCode = ErrorCodeEnum.OK.getIntCode() + "";
		this.msg = IErrorCode.DEFAULT_OK_MESSAGE;
	}


	public PageResult(IErrorCode errorCode, String msg) {
		this.code = errorCode.getResponseStatusCode() + "";
		this.miniCode = errorCode.getIntCode() + "";
		this.msg = msg;
	}

	public PageResult(IErrorCode errorCode, String msg, List<T> listData) {
		this.code = errorCode.getResponseStatusCode() + "";
		this.miniCode = errorCode.getIntCode() + "";
		this.msg = msg;
		this.listData = listData;
	}

    public PageResult(boolean hasNext, int total, int pageNum, List<T> listData) {
		this.hasNext = hasNext;
		this.pageNum = pageNum;
		this.listData = listData;
		this.code =ErrorCodeEnum.OK.getResponseStatusCode() + "";
		this.miniCode = ErrorCodeEnum.OK.getIntCode() + "";
		this.msg =IErrorCode.DEFAULT_OK_MESSAGE;
        this.total=total;
	}

	public boolean isHasNext() {
		return hasNext;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public List<T> getListData() {
		return listData;
	}

	public void setListData(List<T> listData) {
		this.listData = listData;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public static <T> PageResult<T> ok(boolean hasNext, int total, int pageNum, List<T> listData) {
		return new PageResult<T>(hasNext, total, pageNum, listData);
	}

	public static <T> PageResult<T> fail() {
		return new PageResult<T>(ErrorCodeEnum.FAIL, ErrorCodeEnum.FAIL.getMsg());
	}

	public static <T> PageResult<T> fail(IErrorCode errorCode, String msg) {
		return new PageResult<T>(errorCode,msg);
	}

	public static <T> PageResult<T> fail_msg(String msg) {
		return new PageResult<T>(ErrorCodeEnum.FAIL, msg);
	}

	public static <T> PageResult<T> fail(IErrorCode errorCode) {
		return new PageResult<T>(errorCode, errorCode.getMsg());
	}

}
